<?php

namespace App\Admin\Controllers;

use App\Admin\Traits\ModelFormRelations;
use App\Models\Merchant\Gift;

use App\Models\Merchant\GiftLog;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class GiftLogController extends Controller
{
    const CATEGORY=[
        1=>'增加金币',
        2=>'增加积分',
        3=>'兑换车辆',
        4=>'实物领取',
        5=>'虚拟物品',
    ];
    use ModelFormRelations;
    public function index($parent_id){
        
        $this->parent_id = $parent_id;
        return Admin::content(function (Content $content) {
            $gift_name = Gift::where('id',$this->parent_id)->value('name');
            
            $content->header($gift_name.'的领奖记录');
            $content->body($this->grid());
        });
    }
    
    public function edit($parent_id,$id)
    {
        $this->parent_id = $parent_id;
        return Admin::content(function (Content $content) use ($id) {
            
            $content->body($this->form()->edit($id));
        });
    }
    
    /**
     * Create interface.
     *
     * @return Content
     */
    public function create($parent_id)
    {
        $this->parent_id = $parent_id;
        return Admin::content(function (Content $content) {
            
            $content->body($this->form());
        });
    }
    
    protected function grid(){
        return Admin::grid(GiftLog::class, function (Grid $grid) {
            
            $grid->model()->where('merchant_gift_id', $this->parent_id)->orderByDesc('id');
            
            $grid->id();
            $grid->member()->nickname('用户');
            $grid->is_sended('已发至账户')->switch();
            $grid->is_used('用户已领取奖励')->switch();
            $grid->extra('额外信息')->display(function($extra){
                $info = starts_with($extra,'{')?json_decode($extra,true):'无';
                if(is_array($info)){
                    return implode('<br/>',$info);
                }
                return '无';
            });
            $grid->created_at('中奖时间');
            $grid->updated_at('兑换时间');
            $grid->disableCreateButton();
            $grid->disableActions();
            
        });
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(GiftLog::class, function (Form $form) {
            
            $form->switch('is_sended');
            $form->switch('is_used');
        });
    }
}
